package com.zhangzeqin.code.dao.Impl;

import com.zhangzeqin.code.dao.WordDao;
import com.zhangzeqin.code.util.DruidTool;
import com.zhangzeqin.code.util.JdbcUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WordDaoImpl implements WordDao {
    @Override
    public boolean addWord(String wordBook,String word, String meaning) {
        String sql="insert into "+ wordBook +"(word,meaning) values(?,?);";
        //连接数据库
        int i=0;
        try {
            i = JdbcUtil.executeUpdate(sql,  word, meaning);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if(i>0){
            return true;
        }else{
            return false;
        }

    }

    @Override
    public boolean deleteWord(String wordBook,String word) {
        String sql="delete  from "+ wordBook +" where word=? ;";
        //连接数据库
        int i=0;
        try {
            i = JdbcUtil.executeUpdate(sql,  word);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if(i>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean modifyWord(String wordBook,String meaning,String word) {
        String sql="update "+ wordBook +" set meaning=? where word=?;";
        //连接数据库
        int i=0;
        try {
            i = JdbcUtil.executeUpdate(sql, meaning,word);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if(i>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public List<Map> checkWord(String wordBook, String word, String meaning)throws SQLException {
        String sql="select * from ? where word=? or meaning=?;";
        //获取连接
        Connection connection = DruidTool.getConnection();

        //创建预编译statement
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        //占位符赋值,设置?的值
        //preparedStatement.setObject(1,table);
        //创建一个存放map型的集合
        List<Map> list=new ArrayList<>();

        //返回结果集
        ResultSet resultSet = preparedStatement.executeQuery();
//        //获取列的信息对象(可以获取列的名称,根据小角标,获取列的数量)
        ResultSetMetaData metaData=resultSet.getMetaData();
        int columnCount=metaData.getColumnCount();
        while(resultSet.next()){
            Map map=new HashMap();
            //获取指定列下角标的值
            Object key = resultSet.getObject(1);
            Object value = resultSet.getObject(2);
            //将每行数据存到map中
            map.put(key,value);
            //将map存到集合中
            list.add(map);
        }

        if (resultSet.next()){
            //是否收回连接,需要考虑是不是事务
            if(connection.getAutoCommit()){
                //没有开启事务,正常回收连接
                DruidTool.recycleConnection(connection);
            }
            resultSet.close();

        }
        return list;
    }

}
